package com.inspur.gs.sbssdl.sbwhgl.gyjgl.gyjgl.front.tzbfst.voactions;

import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.action.query.BeforeQueryAction;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class seachBFDataBeforeVOAction extends BeforeQueryAction {

    public seachBFDataBeforeVOAction(QueryContext context) {
        super(context);
    }

    @Override
    public void execute() {

        EntityFilter filter = getQueryContext().getFilter();

        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            String bfid = "9f4e8ed6-088b-4665-a03c-90369c636d42";
            IStandardLcp bflcp = iLcpFactory.createLcpByBEId(bfid);

            List<String> ids = bflcp.query().stream().map(s -> s.getID()).collect(Collectors.toList());

            List<String> bftzids = new ArrayList<>();

            for (String id : ids) {
                IEntityDataCollection bfmx = bflcp.retrieve(id).getData().getChilds().get("bfmx");
                if (!bfmx.isEmpty()) {
                   List<String> bftzid = bfmx.stream().map(s -> (String) ((IValuesContainer) s.getValue("gyjtzid")).getValue("gyjtzid")).collect(Collectors.toList());
				   bftzids.addAll(bftzid);
				}
            }

			if (!bftzids.isEmpty()) {
				ArrayList<FilterCondition> FilterConditionS = new ArrayList<>(
						FilterCondition.builder()
								.andNotIn("ID", bftzids)
								.build()
				);
				filter.addFilterConditions(FilterConditionS);
			}
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }
}
